/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
/*
 * FTPServiceClient.java
 *
 * Created on May 8, 2002, 1:33 PM
 */

package com.webdeninteractive.xbotts.client.serviceproviders;

import com.webdeninteractive.xbotts.client.api.client.*;
import com.enterprisedt.net.ftp.*;
import com.webdeninteractive.toolkit.*;
import java.util.regex.*;
import java.net.*;
import java.util.*;

/**
 * ClientService implementation for the FTP Put action
 * @author  jdepons
 * @version $Id: FTPPutClient.java,v 1.1.1.1 2006/01/25 13:51:12 richie93 Exp $ 
 */
public class FTPPutClient extends AbstractClientService {
    
    public ClientResponse runService(ClientRequest clientRequest) throws Exception {        
        
       Map request = clientRequest.getProperties();

        String host = (String)request.get("host");
        String path = (String)request.get("path");
        String username=(String)request.get("username");
        String password=(String)request.get("password");
        String isActiveTransfer=(String)request.get("isActiveTransfer");
        String fileName=(String)request.get("fileName");
        String isUniqueName=(String)request.get("isUniqueName");
        String port=(String)request.get("port");
        String timeout=(String)request.get("timeout");
        String append=(String)request.get("append");

        String destinationRouteID=(String)request.get("destinationRouteID");
        String isBieDestination=(String)request.get("isBieDestination");

        long start = System.currentTimeMillis();                

        if (isUniqueName.equals("true")) {        
            Matcher m = Pattern.compile("^([^\\.]+)(.*)$").matcher(fileName);            
            if (m.find()) {
                fileName = m.group(1) + "_" + new Date().getTime();                
            }                    
        }

        if (isBieDestination.equals("true")) {
            fileName = destinationRouteID + "_" + fileName;
        }


        FTPClient ftpc = new FTPClient(host,Integer.parseInt(port));

        try {
            ftpc.setTimeout(Integer.parseInt(timeout) * 1000);

            if (isActiveTransfer.equals("true")) {
                ftpc.setConnectMode(FTPConnectMode.ACTIVE);
            }else {
                ftpc.setConnectMode(FTPConnectMode.PASV);
            }

            ftpc.user(username);
            ftpc.password(password);

            if (!path.equals("/")) {
                ftpc.chdir(path);
            }    

            ftpc.put( clientRequest.getMessage(),
                      fileName,
                      new Boolean( append ).booleanValue()
                );

            //com.enterprisedt.net.ftp.FTPReply ftpr = ftpc.getLastValidReply();
            //String response = ftpr.getReplyCode() + ": " + ftpr.getReplyText();

            ftpc.quit();
            ClientResponse cre = new ClientResponse();
            
            long end = System.currentTimeMillis();                                                
            return cre;
        }catch (Exception e) {
            try {
                ftpc.quit();
            }catch (Exception e1) {}
            throw e;
        }      
     }        
}

